/* 
 * File:   v4l.h
 * Author: gustavo
 *
 * Created on January 1, 2012, 6:07 AM
 */

#ifndef V4L_H
#define	V4L_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <linux/videodev2.h>
#include <libv4lconvert.h>
#include "../../util/list.h"

#define DEVICE_NAME_MAX_LENGTH 32

	struct buffer {
		void * start;
		size_t length;
	};

	struct v4l_buffers {
		int qtd;
		struct buffer * buffers;
	};

	struct v4l_driver {
		int fd;
		int fmt_emulation;
		struct v4l_buffers v4lbuffers;
		int eq_i;
		int dq_i;

		int width;
		int height;

		struct v4lconvert_data * v4lconv;
		struct v4l2_format format_src;
		int bytesused;

		struct v4l2_control ctrl;

		void * buf_pointer;
		void * buffer_raw;

		//----------------------------
		struct v4l2_capability vcap;
		struct v4l2_input input;
		struct v4l2_standard standard;
		struct v4l2_format format;
		struct v4l2_fmtdesc fmtdest;
		struct v4l2_frmsizeenum frmsize;



	};

	int v4l_get_input(struct v4l_driver * v4ldrv, struct v4l2_input * input);
	int v4l_get_standard(struct v4l_driver * v4ldrv, struct v4l2_standard * standard);
	int v4l_get_format(struct v4l_driver * v4ldrv, struct v4l2_format * format);
	int v4l_get_format_description(struct v4l_driver * v4ldrv, struct v4l2_fmtdesc * fmtdesc);
	int v4l_get_control(struct v4l_driver * v4ldrv, struct v4l2_control * ctrl, int ctrl_id);

	int v4l_enum_devices(char ** dev, int init, int max_dev);
	int v4l_enum_input(struct v4l_driver * v4ldrv, struct v4l2_input * input, int init, int index);
	int v4l_enum_standard(struct v4l_driver * v4ldrv, struct v4l2_standard * standard, int init, int index);
	int v4l_enum_format_desc(struct v4l_driver * v4ldrv, struct v4l2_fmtdesc * fmtdesc, int init, int index);
	int v4l_enum_frame_size(struct v4l_driver * v4ldrv, struct v4l2_frmsizeenum * frmsize, int pixel_format, int init, int index);

	int v4l_enum_control(struct v4l_driver * v4ldrv, struct v4l2_queryctrl * qctrl, int init);
	int v4l_enum_control_user(struct v4l_driver * v4ldrv, struct v4l2_queryctrl * qctrl, int init);
	int v4l_enum_control_private(struct v4l_driver * v4ldrv, struct v4l2_queryctrl * qctrl, int init);
	int v4l_enum_control_menu(struct v4l_driver * v4ldrv, struct v4l2_querymenu * qmenu, int ctrl_id, int index);

	List * v4l_get_device_list(void);

	//struct v4l_driver v4l_open(char * device, int fmt_emulation);
	struct v4l_driver v4l_open_default(const char * device);
	int v4l_close(struct v4l_driver * v4ldrv);

	int v4l_start(struct v4l_driver * v4ldrv);
	int v4l_stop(struct v4l_driver * v4ldrv);

	void * v4l_grab_raw(struct v4l_driver * v4ldrv);
	void * v4l_grab_gray(struct v4l_driver * v4ldrv, char * gray);
	void * v4l_grab_rgb(struct v4l_driver * v4ldrv, char * rgb);


	int v4l_set_input(struct v4l_driver * v4ldrv, int index);
	int v4l_set_standard(struct v4l_driver * v4ldrv, v4l2_std_id id);
	int v4l_set_pixel_format(struct v4l_driver * v4ldrv, int pixel_format);
	int v4l_set_frame_size(struct v4l_driver * v4ldrv, int width, int height);
	int v4l_set_control(struct v4l_driver * v4ldrv, int ctrl_id, int value);










#ifdef	__cplusplus
}
#endif

#endif	/* V4L_H */

